package com.netcombine.freewill.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.netcombine.freewill.constants.PageInfo;
import com.netcombine.freewill.lib.BCrypt;
import com.netcombine.freewill.lib.Paging;
import com.netcombine.freewill.manager.SessionManager;
import com.netcombine.freewill.service.MemberService;
import com.netcombine.freewill.vo.FreewillVo;

@Controller
public class MemberController{
	
	private static final Logger logger = LoggerFactory.getLogger(MemberController.class);
	
	@Autowired
	private MemberService memberService;
	
	@RequestMapping(value = "/login.do")
	public String login(){
		
		return "member/login";
	}
	
	@RequestMapping(value = "/logout.do")
	public String logout(HttpSession session){
		
		SessionManager.logout(session);

		return PageInfo.GO_LOGIN;
	}
	
	@RequestMapping(value = "/login/p.do", method = RequestMethod.POST)
	public String login_proc(HttpSession session, FreewillVo vo)
	{
		FreewillVo memberVO = memberService.selectMemberInfo(vo);
		
		if(memberVO == null){
			
			SessionManager.logout(session);
			
			return PageInfo.GO_LOGIN;
		}
		boolean login_result = BCrypt.checkpw(vo.getAdmin_pw(), memberVO.getAdmin_pw());
		
		if(login_result){
			
			SessionManager.login(session, memberVO);
		}
		
		return PageInfo.GO_MAIN;
	}
		
	@RequestMapping(value = "/member/list.do")
	public String member_list(FreewillVo vo, HttpSession session, Model model){		
		
		if(!SessionManager.isLogin(session)){
			return PageInfo.GO_LOGIN;
		}	

		if(vo.getMode() != null && vo.getMode().equals("insert")){
		
			int result = memberInsert(vo);
			
			if(result == 1){
				model.addAttribute("resultCD", "insert"); 
			}else if(result == 0){
				model.addAttribute("resultCD", "duplication");
			}
			
		}
		else if(vo.getMode() != null && vo.getMode().equals("update")){

			vo.setAdmin_pw(BCrypt.hashpw(vo.getAdmin_pw(), BCrypt.gensalt()));
			int result = memberService.updateMember(vo);
			if(result > 0){
				model.addAttribute("resultCD", "update"); 
			}
			
		}else if(vo.getMode() != null && vo.getMode().equals("delete")){

			int result = memberService.deleteMember(vo);
			if(result > 0){
				model.addAttribute("resultCD", "delete"); 
			}

		}		
		

			if(vo.getPage() == 0) vo.setPage(1);
			int total_cnt = memberService.selectMemberCount(vo);
			logger.info("total_cnt : " + Integer.toString(total_cnt));
		
			Paging paging = new Paging(vo.getPage(), total_cnt, PageInfo.PAGE_SIZE, PageInfo.BLOCK_PAGE, PageInfo.PAGING_SCRIPT);
			
			if(total_cnt != 0){
			
			// paging setting
				vo.setPageSize(paging.getPageSize());
				vo.setStartPosition(paging.getPageStart());			
			
				List<FreewillVo> memberList = memberService.selectMemberList(vo);
			
				model.addAttribute("freewillVo", vo);
				model.addAttribute("memberList", memberList);
			}
				model.addAttribute(PageInfo.CURRENT_MENU, PageInfo.MENU_USER);
				model.addAttribute("paging", paging);

				return "member/list";
	}

			private int memberInsert(FreewillVo vo) {
		
				FreewillVo resultVO = memberService.selectMemberInfo(vo);
				if(resultVO != null) return 0;
					
				vo.setAdmin_pw(BCrypt.hashpw(vo.getAdmin_pw(), BCrypt.gensalt()));
				int result = memberService.insertMember(vo);
			
				return result;
			}
}
	